package com.ling.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ling.springbootinit.common.ResultUtils;
import com.ling.springbootinit.common.WeatherResponse;
import com.ling.springbootinit.mapper.WeatherMapper;
import com.ling.springbootinit.model.dto.weather.ForecastItemRespVO;
import com.ling.springbootinit.model.dto.weather.WeatherRespVO;
import com.ling.springbootinit.model.entity.Weather;
import com.ling.springbootinit.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Weather接口

 */
@RestController
@RequestMapping("/weather")
@Slf4j
public class WeatherController {

    @Resource
    private WeatherService weatherService;
    @Resource
    private WeatherMapper weatherMapper;


    /**
     * 根据city获取天气预报forecast
     */
    @GetMapping("/getWeather")
    public WeatherResponse<WeatherRespVO> getWeatherByCity(String city) {
        WeatherRespVO weatherRespVO = new WeatherRespVO();

//1、查询 List<Weather>
        QueryWrapper<Weather> wrapper = new QueryWrapper<Weather>();
        //name不为空时，组装模糊查询条件
        wrapper.eq("city", city);
        //未删除
        wrapper.eq("isDelete",0); //数据库字段为 isDelete
        //创建时间降序
        wrapper.orderByDesc("createTime"); //数据库字段为 createTime
        List<Weather> weatherList = weatherMapper.selectList(wrapper);
//2、填充List<ForecastItemRespVO>
        //forecast生成
        List<ForecastItemRespVO> forecast = new ArrayList<>();
        for(Weather weather : weatherList){
            ForecastItemRespVO itemRespVO = new ForecastItemRespVO();
            itemRespVO.setDate(weather.getDate());
            itemRespVO.setFengli(weather.getFengli());
            itemRespVO.setFengxiang(weather.getFengxiang());
            itemRespVO.setHigh(weather.getHigh());
            itemRespVO.setLow(weather.getLow());
            itemRespVO.setType(weather.getType());
            forecast.add(itemRespVO);
        }

//3、生成 WeatherRespVO
        weatherRespVO.setCity(city);
        weatherRespVO.setForecast(forecast);
        weatherRespVO.setGanmao("发生感冒机率较低。");
        weatherRespVO.setWendu("20");
        weatherRespVO.setYesterday(null);
        return ResultUtils.successWeather(weatherRespVO);
    }



//   测试，根据城市获取天气
    @GetMapping("/getOneByCity")
    public WeatherResponse<List<Weather>> getOneWeatherByCity(String city) {

        QueryWrapper<Weather> wrapper = new QueryWrapper<Weather>();
        //name不为空时，组装模糊查询条件
        wrapper.eq("city",city);
        //未删除
        wrapper.eq("isDelete",0); //数据库字段为 isDelete
        //创建时间降序
        wrapper.orderByDesc("createTime"); //数据库字段为 createTime
//       weatherMapper.selectOne(wrapper); 如果有多个会报错。
        List<Weather> weatherList = weatherMapper.selectList(wrapper);

        return ResultUtils.successWeather(weatherList);
    }

}
